import Toast from "../../dist/toast/toast";

var util = require('../../utils/util.js')
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        icon: {
            normal: '/images/plain/plain.png',
            active: '//img.yzcdn.cn/icon-active.png'
        },
        countActive: {
            dayActive: true,
            monthActive: false,
            yearActive: false,
        },
        isMainTrueShow: false,
        nowDate: '',
        hideBottom: true,
        avatarUrl: '',
        allPages: '',
        accountType: 1,
        scrollTop: 5,
        //默认是按日的账单统计
        tagId: 0,
        nickName: "",
        page: 1,
        isEmpty: true,
        mainData: {
            isAccountShow: true,
            incomePic: 'account-images/income.png',
            outPic: 'account-images/out-active.png',
        },
        accountList: [],
        scrollHeight: 0,
    },
    outclick() {
        this.setData({
            mainData: {
                isAccountShow: true,
                incomePic: 'account-images/income.png',
                outPic: 'account-images/out-active.png',
            },
            accountType: 1
        })
    },
    incomclick() {
        this.setData({
            mainData: {
                isAccountShow: true,
                incomePic: 'account-images/income-active.png',
                outPic: 'account-images/out.png',
            },
            accountType: 0
        })
    },
    //切换月日年
    switchbtn(e) {
        let id = e.target.dataset.id;
        var dayActive;
        var monthActive;
        var yearActive;
        var tagId;
        switch (id) {
            case '1':
            case '2':
                dayActive = !this.data.countActive.dayActive;
                tagId = 0;
                break;
            case '3':
            case '4':
                monthActive = !this.data.countActive.monthActive;
                tagId = 1;
                break;
            case '5':
            case '6':
                yearActive = !this.data.countActive.yearActive;
                tagId = 2;
                break;
        }
        this.setData({
            countActive: {
                dayActive: dayActive,
                monthActive: monthActive,
                yearActive: yearActive,
                tagId: tagId
            }
        })
        console.log()
    },
    //到分类管理
    toClassify() {
        wx.navigateTo({
            url: '/pages/account/account-classify/account-classify'
        })
    },
    //到新增账单页面
    addAccount(event) {
        wx.navigateTo({
            url: '/pages/account/account-add/account-add?avatarUrl=' + this.data.avatarUrl + "&&nickName=" + this.data.nickName
        })
    },

    //到账单详情编辑页面，或者说账单详情
    showAccountDetails(event){
        //只有选择的是日期展示才给跳转
      if(this.data.countActive.dayActive){
          var currentTime=event.target.dataset.time;
          console.log(currentId+"currentTime");
          wx.navigateTo({
              url: '/pages/account/account-add/account-add?queryTime=' + currentTime
          })
      }
    },
    /**
     * 初始化设置账单详情页的高度
     */
    setAccountHeight: function () {
        let _this = this;
        wx.getSystemInfo({
            success: function (res) {
                _this.setData({
                    scrollHeight: res.windowHeight - 190,
                });
            }
        });
    },

    //初始化获取账单数据
    getAccountList() {
        //默认按照单日获取
        var dataUrl = app.globalData.apiBase +
            "/blog/getAccountDeatils";
        util.ajax(dataUrl, "post", {
            accountType: this.data.accountType,
            tagId: this.data.tagId,
            pageNum: this.data.page,
            pageSize: 10
        }, (respResult, respCode) => {
            if (respCode === '0000') {
                var totalData = [];
                //如果要绑定新加载的数据，那么需要同旧有的数据合并在一起
                if (!this.data.isEmpty) {
                    totalData = this.data.accountList.concat(respResult);
                } else {
                    totalData = respResult;
                    this.data.isEmpty = false;
                }
                this.setData({
                    accountList: totalData
                });
                console.log(this.data.accountList + "数据");
                wx.hideNavigationBarLoading();
                //小程序提供的api，通知页面停止下拉刷新效果 如果发现进入刷新状态，无法停止，可以使用这个api
                wx.stopPullDownRefresh();
            } else {
                Toast("保存失败了，联系老何");
            }
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            avatarUrl: app.globalData.userInfo.avatarUrl,
            nickName: app.globalData.userInfo.nickName,
        })
    },
    toAdd() {
        wx.navigateTo({
            url: '/pages/plain/plain-add/plain-add'
        })
    },
    onChange(event) {
        if (event.detail == 0) {
            this.setData({
                isMainShow: true,
                isSettingShow: false,
                changeCount: 0
            })
        }
    },
    countTime() {
        this.setData({
            nowDate: util.date_time_noTime(new Date().getTime())
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setAccountHeight();
        this.countTime();
        //先清空数据
        this.setData({
            // accountList: [],
            page:1,
        });
        this.getAccountList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    // 上滑加载更多数据
    onReachBottom: function (event) {

    },
    onPullDownRefresh() {
    },
    // 上拉加载更多
    loadMore: function(){
        var self = this;
        // 当前页是最后一页
        if (self.data.page == self.data.allPages){
            self.setData({
                loadMoreData: '已经到顶'
            });
            return;
        }
        var tempCurrentPage = self.data.page;
        tempCurrentPage = tempCurrentPage + 1;
        console.log("触发加载更多，page="+tempCurrentPage);
        self.setData({
            page: tempCurrentPage,
            hideBottom: false
        });
        self.getAccountList();
    }

});
